########################################################################
#  xls2csv - convert excel to csv
#  Copyright (C) 2007,8 Ehud Ben-Reuven
#  udi@benreuven.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
########################################################################
"""xls2csv Excel-File-Name CSV-File-Name

write the Excel file in CSV format
"""


import sys
from optparse import OptionParser
import win32com.client

def xls2csv(finame,foname):
    xl = win32com.client.Dispatch ("Excel.Application")
    wb = xl.Workbooks.Open (finame)
    xlCSVMSDOS=24
    xlCSV=6
    wb.SaveAs(Filename=foname,FileFormat=xlCSV,CreateBackup=0)
    #SaveChanges=0
    wb.Close(SaveChanges=0)

def main():
    parser = OptionParser(__doc__)
    (options, args) = parser.parse_args()
    if len(args) != 2:
        parser.error("Missing file name")
    try:
        xls2csv(args[0],args[1])
    except:
        print "Failed to convert excel file to CSV"
        sys.exit(1)

if __name__ == "__main__":
    main()
